//
//  FileUpLoadAPI.m
//  RainbowFM
//
//  Created by chenli on 14/12/26.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "FileUpLoadAPI.h"
#import "AFAppDotNetAPIClient.h"

@implementation FileUpLoadAPI

-(void)upLoadFileWithServerName:(NSString *)serverName URL:(NSString *)url fileData:(NSData *)fileData_ fileType:(NSString *)type result:(void (^)(BOOL,NSString *,NSString *))res{
    

    [[AFAppDotNetAPIClient sharedClient] POST:url
                                   parameters:nil
                    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                        [formData appendPartWithFileData:fileData_ name:@"file" fileName:@"headImage.jpg" mimeType:@"image/jpeg"];
                    } success:^(NSURLSessionDataTask *task, id responseObject) {
                        if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                            res(YES,responseObject[@"result"][@"picPath"],@"");
                        }
                    } failure:^(NSURLSessionDataTask *task, NSError *error) {
                        
                    }];

    
}

-(void)upLoadFileWithURL:(NSString *)url
                fileData:(NSData *)fileData_
              smallWidth:(NSString *)smallWidth
             smallHeigth:(NSString *)smallHeigth
                fileType:(NSString *)fileType
                fileName:(NSString *)fileName
                  result:(void (^)(BOOL success,NSString *path,NSString *msg))res
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (smallWidth) {
        [params setObject:smallWidth forKey:@"smallWidth"];
    }else{
        [params setObject:@"" forKey:@"smallWidth"];
    }

    if (smallHeigth) {
        [params setObject:smallHeigth forKey:@"smallHeigth"];
    }else{
        [params setObject:@"" forKey:@"smallHeigth"];
    }
    
    if (!fileData_ || !fileType) {
        res(NO,nil,@"缺少 上传数据 或 数据类型");
        return;
    }else
    {
        [params setObject:fileType forKey:@"fileType"];
    }

    
    [[AFAppDotNetAPIClient sharedClient] POST:url
                                   parameters:@{@"serviceName":@"video_file_upload_req",
                                                @"callType":@"002",
                                                @"params":params
                                                }
                    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                        [formData appendPartWithFileData:fileData_
                                                    name:@"file"
                                                fileName:fileName
                                                mimeType:fileType];
                        
                    } success:^(NSURLSessionDataTask *task, id responseObject) {
                        if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                            res(YES,responseObject[@"result"][@"picPath"],@"");
                        }
                    } failure:^(NSURLSessionDataTask *task, NSError *error) {
                        res(NO,nil,@"error upLoadFileWithURL");
                    }];
}

@end
